/**
 * Created by yindume on 2018/12/10.
 */
var express = require('express');
var router = express.Router();
var db = require('../../../libs/mysql');
//统一返回格式
var res_Data;
router.use( function(req, res, next) {
    res_Data = {
        code: 0,
        message: ''
    }
    next();
});
router.get('/',function (req,res) {
    res.render('admin/category/add', {
        userInfo:req.userInfo
    });
});

router.post('/',function(req,res){
    var name = req.body.name;
    db.query(`SELECT 1 FROM category WHERE name='${name}' limit 1`,(err,data)=>{
        if(err){
            res.status(500).send('分类提交').end();
        }else {
            if(data[0]){
                res.render('admin/category/add', {
                    userInfo:req.userInfo,
                    massage :'数据已存在,请重新添加'
                });
            }else {
                db.query(`INSERT INTO category (name) VALUES ('${name}');`,(err)=>{
                    if(err){
                        res.status(500).send('分类插入失败').end();
                    }else {
                        res.render('admin/category/add', {
                            userInfo:req.userInfo,
                            massage :'添加成功'
                        });
                    }
                })
            }
        }
    })
})


module.exports = router;